package com.itheima.sh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @Description:
 * @Version: V1.0
 */
@Configuration
//@ConditionalOnBean(User.class)
//@ConditionalOnMissingClass({"com.itheima.sh.pojo.Pser"})
// 如果容器中存在User bean， 条件满足， RedisConfig会被存到容器，@bean也会被存到容器中
public class RedisConfig {


    @Bean
    public RedisSerializer<Object> redisKeySerializer() {
        return new Jackson2JsonRedisSerializer<Object>(Object.class);
    }

    @Bean
    public RedisSerializer<Object> redisValueSerializer() {
        return new Jackson2JsonRedisSerializer<Object>(Object.class);
    }

    /**
     * RedisTemplate配置
     * @param factory
     */
//    @Bean
//    @ConditionalOnMissingClass
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory
//            , RedisSerializer<Object> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//
//        redisTemplate.setDefaultSerializer(redisValueSerializer);
//        redisTemplate.setKeySerializer(redisKeySerializer);
//        redisTemplate.setHashKeySerializer(redisKeySerializer);
//        redisTemplate.setKeySerializer(redisKeySerializer);
//        redisTemplate.afterPropertiesSet();
//
//        System.out.println("RedisConfig redisTemplate......");
//
//        return redisTemplate;
//    }
}
